﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Web;
using DukeNUS.FacilitiesPlanning.FPSystem.Helpers.Cryptography;

namespace DukeNUS.FacilitiesPlanning.FPSystem.Models
{

    /// <summary>
    /// Application's entity model for the 'users'  table.
    /// </summary>
    public class User
    {

        /// <summary>
        /// Creates a new instance of 
        /// </summary>
        public User()
        {
            UserName = "";
            Password = "";

            FullName = "";
            Designation = "";
            Department = "";

            IsAdmin = false;

            DateCreated = DateTime.Now;
            DateModified = DateTime.Now;
        }

        [Key]
        public int UserID { get; set; }

        [Required, MaxLength(50)]
        public string UserName { get; set; }

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string _password = "";

        [Required]
        public string Password 
        {
            get { return _password; }
            set
            {
                if (value != _password)
                {
                    _password = value;
                    _decryptedPassword = Cryptographer.Decrypt(value);
                }
            }
        }

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string _decryptedPassword = "";

        [NotMapped]
        public string DecryptedPassword
        {
            get
            { return _decryptedPassword; }
            set
            {
                if (value != _decryptedPassword)
                {
                    _decryptedPassword = value;
                    _password = Cryptographer.Encrypt(value);
                }
            }
        }

        [Required, 
        MaxLength(150)]
        public string FullName { get; set; }

        [Required, 
        MaxLength(100)]
        public string Designation { get; set; }

        [Required, 
        MaxLength(100)]
        public string Department { get; set; }

        [Required, 
        MaxLength(100),
        RegularExpression("^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" +
                          "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" +
                          "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$", ErrorMessage = "Invalid email address.")]
        public string Email { get; set; }

        public bool IsAdmin { get; set; }

        public DateTime DateCreated { get; set; }

        public DateTime DateModified { get; set; }

    }
}